package com.sxpi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.common.result.Result;
import com.sxpi.convert.BookingsConvert;
import com.sxpi.mapper.*;
import com.sxpi.model.entity.*;
import com.sxpi.model.page.PageResult;
import com.sxpi.model.vo.BookingListVO;
import com.sxpi.model.vo.BookingsVo;
import com.sxpi.service.BookingsService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@Service
public class BookingsServiceImpl extends ServiceImpl<BookingsMapper, Bookings> implements BookingsService {

    @Resource
    private BookingsMapper bookingsMapper;
    @Resource
    private CostumesMapper costumesMapper;
    @Resource
    private CostumeImagesMapper costumeImagesMapper;
    @Resource
    private StoresMapper storesMapper;
    @Resource
    private UsersMapper usersMapper;
//

    @Override
    public Boolean submitBooking(Bookings bookings) {
        // 保持原有实现不变


        int insert = bookingsMapper.insert(bookings);
        return insert > 0;
    }

    @Override
    public Result<PageResult<BookingsVo>> getBookingList(Long userId, Integer pageNo, Integer pageSize, Integer status , Integer isRelease) {
         Page<Bookings> pages = new Page<>(pageNo,pageSize);
        // 1. 查询用户的所有预约记录
        LambdaQueryWrapper<Bookings> bookingsWrapper = new LambdaQueryWrapper<>();
        bookingsWrapper.eq(Bookings::getUserId, userId);
        bookingsWrapper.eq(!Objects.isNull(status),Bookings::getStatus, status);
        bookingsWrapper.eq(!Objects.isNull(isRelease),Bookings::getIsRelease, isRelease);
        bookingsWrapper.orderByDesc(Bookings::getCreatedTime);
         Page<Bookings> bookingsPage = bookingsMapper.selectPage(pages, bookingsWrapper);
         List<Bookings> records = bookingsPage.getRecords();
         log.info("records:{}",records);
         List<BookingsVo> bookingsVos = BookingsConvert.INSTANCE.convertEntityToVo(records);
        bookingsVos.forEach(bookingsVo -> {
            log.info("bookingsVo:{}",bookingsVo);
            CostumeImages costumeImages = costumeImagesMapper.selectOne(new LambdaQueryWrapper<CostumeImages>().eq(CostumeImages::getCostumeId, bookingsVo.getCostumeId()).eq(CostumeImages::getIsMain, 1));
             Costumes costumes = costumesMapper.selectOne(new LambdaQueryWrapper<Costumes>().eq(Costumes::getId, bookingsVo.getCostumeId()));
            log.info("costumeImages:{}",costumeImages);
            if (costumeImages == null){
                return;
            }
            LambdaQueryWrapper<Stores> eq = new LambdaQueryWrapper<Stores>().eq(Stores::getId, bookingsVo.getStoreId());
            Stores stores = storesMapper.selectOne(eq);
            if (stores == null){
                return;
            }
            bookingsVo.setProductImage(costumeImages.getUrl());
            bookingsVo.setProductName(costumes.getName());
            bookingsVo.setDescription(costumes.getDescription());
            bookingsVo.setStoresName(stores.getName());

        });
         PageResult<BookingsVo> bookingsVoPageResult = new PageResult<>();
         bookingsVoPageResult.setPageNo(pageNo);
         bookingsVoPageResult.setPageSize(pageSize);
         bookingsVoPageResult.setResult(bookingsVos);
         bookingsVoPageResult.setTotal((int) bookingsPage.getTotal());
        return Result.ok(bookingsVoPageResult);
    }

    @Override
    public Result<Map<String,Object>> getBookingByOrders(String orders) {
        LambdaQueryWrapper<Bookings> eq = new LambdaQueryWrapper<Bookings>().eq(Bookings::getOrderNumber, orders);
        Bookings bookings = bookingsMapper.selectOne(eq);
        log.info("bookings:{}",bookings);
        Costumes costumes = costumesMapper.selectOne(new LambdaQueryWrapper<Costumes>().eq(Costumes::getId, bookings.getCostumeId()));
        Stores stores = storesMapper.selectOne(new LambdaQueryWrapper<Stores>().eq(Stores::getId, bookings.getStoreId()));
        CostumeImages costumeImages = costumeImagesMapper.selectOne(new LambdaQueryWrapper<CostumeImages>().eq(CostumeImages::getCostumeId, bookings.getCostumeId()).eq(CostumeImages::getIsMain, 1));


        Users photographer = new Users();
        Users makeupArtist = new Users();
        if(!Objects.isNull(bookings.getPhotographerId())) {
            //查询摄影师信息
             photographer = usersMapper.selectOne(new LambdaQueryWrapper<Users>().eq(Users::getId, bookings.getPhotographerId()));
        }
        if (!Objects.isNull(bookings.getMakeupArtistId())){
            //查询化妆师信息
             makeupArtist = usersMapper.selectOne(new LambdaQueryWrapper<Users>().eq(Users::getId, bookings.getMakeupArtistId()));
        }




         //存入预约人信息
        Map<String, Object> bookingInfo = new HashMap<>();
        bookingInfo.put("bookingDate", bookings.getBookingDate());
        bookingInfo.put("bookingTime", bookings.getBookingTime());
        bookingInfo.put("bookingStatus", bookings.getStatus());
        bookingInfo.put("phone", bookings.getPhone());
        bookingInfo.put("name", bookings.getName());
        bookingInfo.put("address", stores.getAddress() );
        bookingInfo.put("storesName", stores.getName());
        bookingInfo.put("storePhone", stores.getPhone());
        bookingInfo.put("photographer", photographer.getUsername());
        bookingInfo.put("makeupArtist", makeupArtist.getUsername());

        //存入预约商品信息
        Map<String, Object> costumeInfo = new HashMap<>();
        costumeInfo.put("name", costumes.getName());
        costumeInfo.put("price", costumes.getPrice());
        costumeInfo.put("description", costumes.getDescription());
        costumeInfo.put("url", costumeImages.getUrl());

        //存入预约订单信息
        Map<String, Object> orderInfo = new HashMap<>();
        orderInfo.put("orderNumber", bookings.getId());
        orderInfo.put("createdTime", bookings.getCreatedTime());
        orderInfo.put("pay", bookings.getPay());


        Map<String, Object> map = new HashMap<>();
        map.put("bookingInfo", bookingInfo);
        map.put("costumeInfo", costumeInfo);
        map.put("orderInfo", orderInfo);
        return Result.ok(map);
    }

    @Override
    public Result<BookingsVo> release(Long userId, Integer isRelease) {
        LambdaQueryWrapper<Bookings>  books = new LambdaQueryWrapper<>();
        books.eq(Objects.isNull(userId),Bookings::getUserId,userId);
        books.eq(Objects.isNull(isRelease),Bookings::getIsRelease,isRelease);

        List<Bookings> bookings = bookingsMapper.selectList(books);
        List<BookingsVo> bookingsVos = BookingsConvert.INSTANCE.convertVoToEntityList(bookings);
        return null;
    }
}